/*
 * This file is part of cve-check-tool
 * Copyright (C) 2015 Intel Corporation
 *
 * cve-check-tool is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#define _GNU_SOURCE
#include <check.h>
#include <stdio.h>
#include <stdlib.h>

#include "cve-string.c"
#include "util.c"
#include "util.h"

#include "config.h"
#include "hashmap.c"
#include "plugins/jira/jira.c"
#include "plugins/jira/jira.h"
#include "template.c"

const char *nvd_file = "nvd.db";

#define DEFAULT_CONFIG_FILE DEFAULT_PATH "/cve-check-tool.conf"
#define TEST_CONFIG_FILE TOP_DIR "/tests/dummy_data/plugins/jira/cve-check-tool.conf"
#define TEST_JSON_FILE TOP_DIR "/tests/dummy_data/plugins/jira/jira_test_issues.json"

/* Check the plugin init function which includes parsing the config file */
START_TEST(cve_jira_plugin_init_function)
{
        bool ret = false;
        ret = init_jira_plugin(NULL, TEST_CONFIG_FILE);
        fail_if(!ret, "Failed to initialize JIRA plugin");
        destroy_jira_plugin();
}
END_TEST

/* Check the JIRA issues family of functions */
START_TEST(cve_jira_plugin_jira_issues_functions)
{
        bool ret = false;
        int count = 0;
        GSList *jira_issues = NULL;
        ret = init_jira_plugin(NULL, TEST_CONFIG_FILE);
        fail_if(!ret, "Failed to initialize JIRA plugin");
        ret = get_jira_issues_file(TEST_JSON_FILE, &jira_issues);
        fail_if(!ret, "Failed load JIRA issues from file");
        count = get_jira_issues_count(jira_issues);
        fail_if(count != 10, "Failed expected count of 10");
        show_jira_issues(jira_issues);
        free_jira_issues(&jira_issues);
        free_jira_issues(NULL);
        destroy_jira_plugin();
}
END_TEST

/* No explicit get fuctions to verify, but let's still exercise the code */
START_TEST(cve_jira_plugin_set_functions)
{
        bool ret = false;
        int i = 0;
        ret = init_jira_plugin(NULL, TEST_CONFIG_FILE);
        fail_if(!ret, "Failed to initialize JIRA plugin");
        for (i = 0; i < 2; i++) {
                if (i == 1) {
                        destroy_jira_plugin();
                }
                set_timeout_secs(-1);
                set_timeout_secs(0);
                set_timeout_secs(123);
                set_timeout_secs(3.14);
                set_url("http://foo");
                set_url("");
                set_url(NULL);
                set_user("someuser");
                set_user("");
                set_user(NULL);
                set_verbose(true);
                set_verbose(false);
        }
        destroy_jira_plugin();
}
END_TEST

/* Check the save functions */
START_TEST(cve_jira_plugin_save_functions)
{
        bool ret = false;
        int count = 0;
        GSList *jira_issues = NULL;
        ret = init_jira_plugin(NULL, TEST_CONFIG_FILE);
        fail_if(!ret, "Failed to initialize JIRA plugin");
        ret = get_jira_issues_file(TEST_JSON_FILE, &jira_issues);
        fail_if(!ret, "Failed load JIRA issues from file");
        count = get_jira_issues_count(jira_issues);
        fail_if(count != 10, "Failed expected count of 10");
        ret = save_jira_issues_csv(jira_issues, "test_jira_issues.csv");
        if (!ret || !cve_file_exists("test_jira_issues.csv")) {
                fail("Failed to save CSV to a file");
        } else {
                remove("test_jira_issues.csv");
        }
        ret = save_jira_issues_xml(jira_issues, "test_jira_issues.xml");
        if (!ret || !cve_file_exists("test_jira_issues.xml")) {
                fail("Failed to save XML to a file");
        } else {
                remove("test_jira_issues.xml");
        }
        ret = save_jira_issues_csv(jira_issues, "");
        fail_if(ret, "Negative save to CSV file test should have failed");
        ret = save_jira_issues_csv(jira_issues, NULL);
        fail_if(ret, "Negative save to CSV file test should have failed");
        ret = save_jira_issues_csv(NULL, "");
        fail_if(ret, "Negative save to CSV file test should have failed");
        ret = save_jira_issues_csv(NULL, NULL);
        fail_if(ret, "Negative save to CSV file test should have failed");
        ret = save_jira_issues_csv(NULL, "test_jira_issues.csv");
        fail_if(ret, "Negative save to CSV file test should have failed");
        if (cve_file_exists("test_jira_issues.csv")) {
                remove("test_jira_issues.csv");
                fail("Negative save to CSV file test should not have not create file");
        }
        ret = save_jira_issues_xml(jira_issues, "");
        fail_if(ret, "Negative save to XML file test should have failed");
        ret = save_jira_issues_xml(jira_issues, NULL);
        fail_if(ret, "Negative save to XML file test should have failed");
        ret = save_jira_issues_xml(NULL, "");
        fail_if(ret, "Negative save to XML file test should have failed");
        ret = save_jira_issues_xml(NULL, NULL);
        fail_if(ret, "Negative save to XML file test should have failed");
        ret = save_jira_issues_xml(NULL, "test_jira_issues.xml");
        fail_if(ret, "Negative save to XML file test should have failed");
        if (cve_file_exists("test_jira_issues.xml")) {
                remove("test_jira_issues.xml");
                fail("Negative save to XML file test should not have not create file");
        }
        ret = save("fleischkuekle", "test_jira_save.txt");
        if (!ret || !cve_file_exists("test_jira_save.txt")) {
                fail("Failed to save text to a file");
        } else {
                remove("test_jira_save.txt");
        }
        ret = save("", "");
        fail_if(ret, "Negative save to text file test should have failed");
        ret = save(NULL, "");
        fail_if(ret, "Negative save to text file test should have failed");
        ret = save("", NULL);
        fail_if(ret, "Negative save to text file test should have failed");
        ret = save("", "test_jira_save.txt");
        if (cve_file_exists("test_jira_save.txt")) {
                remove("test_jira_save.txt");
                fail("Negative save to text file test should not have not create file");
        }
        ret = save(NULL, "test_jira_save.txt");
        if (cve_file_exists("test_jira_save.txt")) {
                remove("test_jira_save.txt");
                fail("Negative save to text file test should not have not create file");
        }
        destroy_jira_plugin();
}
END_TEST

static Suite *core_suite(void)
{
        Suite *s = NULL;
        TCase *tc = NULL;

        s = suite_create("cve_jira_plugin");
        tc = tcase_create("cve_jira_plugin_functions");
        tcase_add_test(tc, cve_jira_plugin_init_function);
        tcase_add_test(tc, cve_jira_plugin_jira_issues_functions);
        tcase_add_test(tc, cve_jira_plugin_set_functions);
        tcase_add_test(tc, cve_jira_plugin_save_functions);
        suite_add_tcase(s, tc);
        return s;
}

int main(void)
{
        Suite *s;
        SRunner *sr;
        int fail;

        s = core_suite();
        sr = srunner_create(s);
        srunner_run_all(sr, CK_VERBOSE);
        fail = srunner_ntests_failed(sr);
        srunner_free(sr);

        if (fail > 0) {
                return EXIT_FAILURE;
        }

        return EXIT_SUCCESS;
}

/*
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
 *
 * Local variables:
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * vi: set shiftwidth=8 tabstop=8 expandtab:
 * :indentSize=8:tabSize=8:noTabs=true:
 */
